{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2d39ae18",
   "metadata": {},
   "source": [
    "# 调用手机摄像头，逐帧实时拍摄\n",
    "\n",
    "同济子豪兄 2023-6-25"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b1583e5",
   "metadata": {},
   "source": [
    "## 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8de9bb2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from cvs import *"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54e2de95",
   "metadata": {},
   "source": [
    "## 初始化摄像头"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "81067a1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 摄像头ID 0-后置 1-前置\n",
    "Camera_ID = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1fd0fe7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "remi.server      INFO     Started httpserver http://0.0.0.0:10678/\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('app runs on port:', 10678)\n",
      "open the cam:0 ...\n"
     ]
    }
   ],
   "source": [
    "cap = cvs.VideoCapture(Camera_ID)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d391944",
   "metadata": {},
   "source": [
    "## 手机摄像头逐帧实时拍摄"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ff4635d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "remi.server.ws   INFO     connection established: ('127.0.0.1', 39290)\n",
      "remi.server.ws   INFO     handshake complete\n",
      "remi.server.ws   ERROR    error parsing websocket\n",
      "Traceback (most recent call last):\n",
      "  File \"/usr/local/lib/python3.7/dist-packages/remi/server.py\", line 269, in on_message\n",
      "    callback = get_method_by_name(runtimeInstances[widget_id], function_name)\n",
      "  File \"/usr/lib/python3.7/weakref.py\", line 137, in __getitem__\n",
      "    o = self.data[key]()\n",
      "KeyError: '543216134856'\n",
      "Corrupt JPEG data: premature end of data segment\n",
      "Corrupt JPEG data: bad Huffman code\n"
     ]
    }
   ],
   "source": [
    "while True:\n",
    "    img_bgr = cap.read()\n",
    "    \n",
    "    if img_bgr is None:\n",
    "        continue\n",
    "    \n",
    "    cvs.imshow(img_bgr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a885bf03",
   "metadata": {},
   "source": [
    "## 点击`重启kernel`关闭摄像头"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "06a0bb98",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
